<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// import echarts from 'echarts'
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: [Object, Array],
    title: String
  },
  data() {
    return {
      chart: null
      // chartDataList:this.chartData?this.chartData:{}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        color: this.title == '聘用职称统计' || this.title == '聘用职级统计' ? 'rgba(31, 204, 175, 1)' : 'rgba(38, 147, 255, 1)',
        title: {
          text: this.title,
          left: 'left',
          top: '0%',
          textStyle: {
            color: '#000', // 标题颜色
            fontWeight: 'bold',
            fontSize: 16
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 60,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.chartData.dataName, // ['专业名称1', '专业名称2', '专业名称3', '专业名称4', '专业名称5', '专业名称6', '专业名称7'],
          axisTick: {
            show: false,
            alignWithLabel: true
          },
          axisLabel: {
            textStyle: {
              color: 'rgba(0, 0, 0, 0.40)' // 这里设置颜色
            },
            interval: 0 // 强制显示完整
          }
        }],
        yAxis: [{
          name: '人数',
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: 'rgba(0, 0, 0, 0.40)' // 这里设置颜色
            },
            interval: 0 // 强制显示完整
          },
          nameTextStyle: {
            color: 'rgba(0, 0, 0, 0.40)'
          }

        }],
        series: [{
          name: '人数',
          type: 'bar',
          stack: 'vistors',
          barWidth: '30%',
          data: this.chartData.dataList, // [79, 52, 200, 334, 390, 330, 220],
          animationDuration,
          label: {
            normal: {
              position: 'top', // 在内部显示，inside、 outseide 是在外部显示
              show: true
            }
          },
          itemStyle: {
            barBorderRadius: [4, 4, 0, 0]
          }
        }]
      })
    }
  }
}
</script>
